﻿using System;
using System.Collections.Specialized;
using System.Runtime.Remoting.Messaging;

namespace OperationPlugins.Applications
{
    [Serializable]
    public class HttpRequestHeaders : MarshalByRefObject, ILogicalThreadAffinative
    {
        private readonly NameValueCollection _headers;

        public static HttpRequestHeaders Bind(NameValueCollection headers)
        {
            return new HttpRequestHeaders(headers);
        }

        /// <summary>
        /// Initializes a new instance of the HttpRequestHeaders class.
        /// </summary>
        /// <param name="headers">A collection containing key value pairs.</param>
        public HttpRequestHeaders(NameValueCollection headers)
        {
            if (headers == null)
            {
                throw new ArgumentNullException("headers");
            }

            _headers = headers;
        }

        /// <summary>
        /// AppId
        /// </summary>
        public virtual string AppId
        {
            get { return GetValue("X-OperationPlugins-AppId"); }
            set { SetValue("X-OperationPlugins-AppId", value); }
        }

        /// <summary>
        /// Username
        /// </summary>
        public virtual string Username
        {
            get { return GetValue("X-OperationPlugins-Username"); }
            set { SetValue("X-OperationPlugins-Username", value); }
        }

        /// <summary>
        /// Password
        /// </summary>
        public virtual string Password
        {
            get { return GetValue("X-OperationPlugins-Password"); }
            set { SetValue("X-OperationPlugins-Password", value); }
        }

        /// <summary>
        /// Service
        /// </summary>
        public virtual string Service
        {
            get { return GetValue("X-OperationPlugins-Service"); }
            set { SetValue("X-OperationPlugins-Service", value); }            
        }

        /// <summary>
        /// OperationId
        /// </summary>
        public virtual string OperationId
        {
            get { return GetValue("X-OperationPlugins-OperationId"); }
            set { SetValue("X-OperationPlugins-OperationId", value); }
        }

        /// <summary>
        /// BranchId
        /// </summary>
        public virtual string BranchId
        {
            get { return GetValue("X-OperationPlugins-BranchId"); }
            set { SetValue("X-OperationPlugins-BranchId", value); }
        }

        /// <summary>
        /// PayloadContentAlias
        /// </summary>
        public virtual string PayloadContentAlias
        {
            get { return GetValue("X-OperationPlugins-Payload-Content-Alias"); }
            set { SetValue("X-OperationPlugins-Payload-Content-Alias", value); }
        }

        /// <summary>
        /// PayloadContentType
        /// </summary>
        public virtual string PayloadContentType
        {
            get { return GetValue("X-OperationPlugins-Payload-Content-Type"); }
            set { SetValue("X-OperationPlugins-Payload-Content-Type", value); }            
        }

        protected virtual string GetValue(string name)
        {
            string value = null;

            foreach (string key in _headers.AllKeys)
            {
                if (string.Equals(key, name, StringComparison.InvariantCultureIgnoreCase))
                {
                    value = _headers.Get(key);
                    break;
                }
            }

            return value;
        }

        protected virtual void SetValue(string name, string value)
        {
            foreach (string key in _headers.AllKeys)
            {
                if (string.Equals(key, name, StringComparison.InvariantCultureIgnoreCase))
                {
                    _headers.Remove(key);
                }
            }

            if (string.IsNullOrEmpty(value) == false)
            {
                _headers.Add(name, value);    
            }
        }

    }
}
